package com.cpan.android.sms.provider;

import com.cpan.android.sms.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMSProvider extends Activity implements OnClickListener {
	final static String TAG = "SMSProvider";
	final static boolean debug = true;
	EditText txtPhoneNo;
	EditText txtMessage;
	Button btnSendSMS;

	protected void onCreate(Bundle savedInstanceState) {
		if (debug)
			Log.e(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.z_write_sms_normal);
		//peter .....
		
		txtPhoneNo = (EditText) findViewById(R.id.txt_phone_no);
		txtMessage = (EditText) findViewById(R.id.txt_message);
		btnSendSMS = (Button) findViewById(R.id.btn_send_sms);
		btnSendSMS.setOnClickListener(this);
	}

	public void onClick(View v) {
		if (debug)
			Log.e(TAG, "onClick");
		final String phoneNo = txtPhoneNo.getText().toString();
		final String messages = txtMessage.getText().toString();
		if (phoneNo.length() <= 0) {
			Toast.makeText(this, R.string.no_phone_no, 300).show();
		} else if (messages.length() <= 0) {
			Dialog digEmptyMessage = new AlertDialog.Builder(SMSProvider.this)
					.setTitle(R.string.wraning).setMessage(
							R.string.empty_message_warnging).setNegativeButton(
							R.string.cancel, null).setPositiveButton(
							R.string.ok, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									sendSMS(phoneNo, messages);
								}
							}).create();
			digEmptyMessage.show();
		} else {
			sendSMS(phoneNo, messages);
		}

	}

	public void sendSMS(String phoneNo, String messages) {
		if (debug)
			Log.e(TAG, "sendSMS");
		String SENT = "SMS_SENT";
		String DELIVERED = "SMS_DELIVERED";

		PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
				SENT), 0);

		PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
				new Intent(DELIVERED), 0);

		registerReceiver(new BroadcastReceiver() {
			public void onReceive(Context context, Intent intent) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS sent",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(getBaseContext(), "Generic failure",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(getBaseContext(), "No service",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(getBaseContext(), "Null PDU",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(getBaseContext(), "Radio off",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(SENT));

		registerReceiver(new BroadcastReceiver() {
			public void onReceive(Context context, Intent intent) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS delivered",
							Toast.LENGTH_SHORT).show();
					break;
				case Activity.RESULT_CANCELED:
					Toast.makeText(getBaseContext(), "SMS not delivered",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(DELIVERED));

		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNo, null, messages, sentPI, deliveredPI);
	}

}
